<template>
   <div class="user-info-container">
      <el-form ref="userRef" :model="form" :rules="rules" label-width="100px" class="user-info-form">
         <el-form-item label="用户昵称" prop="nickName">
            <el-input 
               v-model="form.nickName" 
               maxlength="30"
               class="custom-input"
            >
               <template #prefix>
                  <el-icon><User /></el-icon>
               </template>
            </el-input>
         </el-form-item>
         <el-form-item label="手机号码" prop="phonenumber">
            <el-input 
               v-model="form.phonenumber" 
               maxlength="11"
               class="custom-input"
            >
               <template #prefix>
                  <el-icon><Phone /></el-icon>
               </template>
            </el-input>
         </el-form-item>
         <el-form-item label="邮箱" prop="email">
            <el-input 
               v-model="form.email" 
               maxlength="50"
               class="custom-input"
            >
               <template #prefix>
                  <el-icon><Message /></el-icon>
               </template>
            </el-input>
         </el-form-item>
         <el-form-item label="性别">
            <el-radio-group v-model="form.sex" class="custom-radio-group">
               <el-radio value="0">
                  <el-icon><Male /></el-icon>男
               </el-radio>
               <el-radio value="1">
                  <el-icon><Female /></el-icon>女
               </el-radio>
            </el-radio-group>
         </el-form-item>
         <el-form-item class="form-buttons">
            <el-button type="primary" @click="submit" class="submit-btn">
               <el-icon><Check /></el-icon>保存
            </el-button>
            <el-button @click="close" class="cancel-btn">
               <el-icon><Close /></el-icon>关闭
            </el-button>
         </el-form-item>
      </el-form>
   </div>
</template>

<script setup>
import { updateUserProfile } from "@/api/system/user";

const props = defineProps({
  user: {
    type: Object
  }
});

const { proxy } = getCurrentInstance();

const form = ref({});
const rules = ref({
  nickName: [{ required: true, message: "用户昵称不能为空", trigger: "blur" }],
  email: [{ required: true, message: "邮箱地址不能为空", trigger: "blur" }, { type: "email", message: "请输入正确的邮箱地址", trigger: ["blur", "change"] }],
  phonenumber: [{ required: true, message: "手机号码不能为空", trigger: "blur" }, { pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/, message: "请输入正确的手机号码", trigger: "blur" }],
});

/** 提交按钮 */
function submit() {
  proxy.$refs.userRef.validate(valid => {
    if (valid) {
      updateUserProfile(form.value).then(response => {
        proxy.$modal.msgSuccess("修改成功");
        props.user.phonenumber = form.value.phonenumber;
        props.user.email = form.value.email;
      });
    }
  });
};

/** 关闭按钮 */
function close() {
  proxy.$tab.closePage();
};

// 回显当前登录用户信息
watch(() => props.user, user => {
  if (user) {
    form.value = { nickName: user.nickName, phonenumber: user.phonenumber, email: user.email, sex: user.sex };
  }
},{ immediate: true });
</script>

<style lang="scss" scoped>
.user-info-container {
  padding: 20px;
  max-width: 600px;
  margin: 0 auto;
}

.user-info-form {
  background: var(--el-bg-color);
  padding: 30px;
  border-radius: 12px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.04);
}

.custom-input {
  :deep(.el-input__wrapper) {
    padding: 8px 12px;
    box-shadow: 0 0 0 1px var(--el-border-color) inset;
    transition: all 0.3s ease;
    
    &:hover {
      box-shadow: 0 0 0 1px var(--el-color-primary) inset;
    }
    
    &.is-focus {
      box-shadow: 0 0 0 1px var(--el-color-primary) inset;
    }
  }
  
  :deep(.el-input__prefix) {
    color: var(--el-text-color-secondary);
    margin-right: 8px;
  }
}

.custom-radio-group {
  :deep(.el-radio) {
    margin-right: 24px;
    padding: 8px 16px;
    border-radius: 6px;
    transition: all 0.3s ease;
    
    &:hover {
      background: var(--el-color-primary-light-9);
    }
    
    .el-icon {
      margin-right: 4px;
      font-size: 16px;
    }
    
    &.is-checked {
      background: var(--el-color-primary-light-9);
      
      .el-radio__label {
        color: var(--el-color-primary);
      }
    }
  }
}

.form-buttons {
  margin-top: 30px;
  text-align: center;
  
  .el-button {
    padding: 12px 24px;
    font-size: 14px;
    border-radius: 6px;
    transition: all 0.3s ease;
    
    .el-icon {
      margin-right: 4px;
    }
  }
  
  .submit-btn {
    background: var(--el-color-primary);
    border-color: var(--el-color-primary);
    
    &:hover {
      transform: translateY(-1px);
      box-shadow: 0 4px 12px rgba(var(--el-color-primary-rgb), 0.3);
    }
  }
  
  .cancel-btn {
    margin-left: 16px;
    
    &:hover {
      background: var(--el-color-danger-light-9);
      border-color: var(--el-color-danger);
      color: var(--el-color-danger);
    }
  }
}
</style>
